package com.dms.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dms.bean.Student;
import com.dms.dao.StudentDao;
import com.dms.model.StudentModel;

/**
 * Servlet implementation class StudentServlet
 */
@WebServlet("/StudentServlet")
public class StudentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StudentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");
		 response.setCharacterEncoding("UTF-8");
	   	 request.setCharacterEncoding("UTF-8");
	   	HttpSession session = request.getSession();
		Student stu = (Student) session.getAttribute("student");
	   	String action = request.getParameter("act");
		if (action == null)
			action = "";
		if (action.equals("add")) {
			this.add(request, response,stu);
		}
		if (action.equals("list")) {
			this.list(request, response,stu);
		}
		if (action.equals("edit")) {
			this.edit(request, response,stu);
		}
		if (action.equals("update")) {
			this.update(request, response,stu);
		}
		if (action.equals("delete")) {
			this.delete(request, response,stu);
		}
	}
protected void add(HttpServletRequest request, HttpServletResponse response,Student stu) throws ServletException, IOException {
		
		String stuID = request.getParameter("stuID");
		String name = request.getParameter("name");
		String password = stuID;
		String dormID = request.getParameter("dormID");
		if (stuID == null || "".equals(stuID) || name == null || "".equals(name) || dormID == null || "".equals(dormID)) {
			request.setAttribute("message", "天哪噜,缺数据或数据错误(⊙o⊙)…");
			request.getRequestDispatcher("addStudent.jsp").forward(request, response);
		}
		boolean val = StudentModel.add(stuID, name, password, dormID);
		if (!val) {
			request.setAttribute("message", "天哪噜,失败啦(⊙o⊙)…");
			request.getRequestDispatcher("addsStudent.jsp").forward(request, response);
		} else {
			response.sendRedirect("StudentServlet?act=list");
		}
	}
	protected void list(HttpServletRequest request, HttpServletResponse response,Student stu) throws ServletException, IOException {
		List<Student> students = StudentDao.list();
		request.setAttribute("list", students);
		request.getRequestDispatcher("student.jsp").forward(request, response);
	}
	protected void edit(HttpServletRequest request, HttpServletResponse response,Student stu) throws ServletException, IOException {
		String stuID = request.getParameter("stuID");
		Student student =  StudentDao.find(stuID);
		request.setAttribute("student", student);
		request.getRequestDispatcher("editStudent.jsp").forward(request, response);
	}
	protected void update(HttpServletRequest request, HttpServletResponse response,Student stu) throws ServletException, IOException {
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		String dormID = request.getParameter("dormID");
		if (name == null || "".equals(name) || password == null || "".equals(password) || dormID == null || "".equals(dormID)) {
			request.setAttribute("message", "天哪噜,缺数据或数据错误(⊙o⊙)…");
			request.getRequestDispatcher("addStudent.jsp").forward(request, response);
		}
		String stuID = request.getParameter("stuID");
		Student student = new Student();
		student.setStuID(stuID);
		student.setName(name);
		student.setPassword(password);
		student.setDormID(dormID);
		boolean var =false;
		try {
		      var =  StudentDao.save(student);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(!var){
			request.setAttribute("message", "修改失败 @_@");
			request.getRequestDispatcher("editStudent.jsp").forward(request, response);
		}else{
			response.sendRedirect("StudentServlet?act=list");
		}
	}
	protected void delete(HttpServletRequest request, HttpServletResponse response,Student stu) throws ServletException, IOException {
		String id = request.getParameter("stuID");
		boolean var =false;
		try {
		      var =  StudentDao.delete(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(!var){
			request.setAttribute("message", "删除失败 @_@");
			request.getRequestDispatcher("editStudent.jsp").forward(request, response);
		}else{
			response.sendRedirect("StudentServlet?act=list");
		}
	}

}
